package com.pumpkin.esapi.utils;

import com.pumpkin.esapi.pojo.Content;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * 解析网页工具类
 */
@Component
public class HttpParseUtil {
    public static void main(String[] args) throws IOException {
        new HttpParseUtil().parseJD("vue").forEach(System.out::println);
    }

    /**
     * 输入想要获取的关键字从JD获取内容
     * @param keyword
     * @return
     * @throws IOException
     */
    public List<Content> parseJD(String keyword) throws IOException {
        //解析地址：
        String url = "https://search.jd.com/Search?keyword=" + keyword + "&enc=utf-8";
        String str = "__jdv=95931165|direct|-|none|-|1712540199099; __jdu=1712540199099870852045; wlfstk_smdl=klcqfl8xnu1oel34df5cqpo9eqguij73; TrackID=15uAQnk8kyiZG_FhmPqG2BdtSMjZtjKzRwq9ksgh32zlkU_j9ocsnzxs6BlLPIMNfyeIOXiRMQyZMLwbZ4hROBtZzGj23yMUo_s42jWJ2a6k; thor=0EF4FA54FEC909F60FA00B7234B310AB14C79CAA4074A4CAA75EE8AB67FD7EB1ACA8D2A9C2DB17358018A31B813D195204BB34DA6C71CF77D5F4554FB270AE9127AEE69029C8D100D1D973092D74AFE27690D9FC8A125CF4D94341DEE904625F8598F0F7D533CCB952A0F530406701AF4F0D9C330F9A5EAB720E74102408D981F68F1A2502D265C07E2367F2777853A74F4C3E2F7D766A9F8D91029350A4462A; flash=2_JNhyexXO4jMO4bm2bFnP4m-Ipv34auJsj9Ry-AbMSJdjm1lwZ6L9RkFJUxfB4oyIYYzQUBYex3bqViGTPjkLgMlHhugDevSqViKykVCfIT5*; pinId=R0TyD1RTfzdst1jcF0xRyLV9-x-f3wj7; pin=jd_74fe58f6c4d5a; unick=%E5%95%B5%E5%95%B5%E7%9A%84%E5%A4%A7%E6%9C%8B%E5%8F%8B; ceshi3.com=000; _tp=F%2FNkQ82sPkBHFugNWyMPmE7LM7rCp7F4R9y%2FyRsoKH0%3D; _pst=jd_74fe58f6c4d5a; ipLoc-djd=1-2800-0-0; jsavif=1; jsavif=1; xapieid=jdd03FF4E4BDXGZSCBQK57J2TX6ZV4SREWT4PR5ZTPR5BPS3MSTOCFHOTBP7M54NDGDSJ4DNM7GCDWLKYDSNAHWHKJOVO3IAAAAMOXNL5CYQAAAAAD7ZVAOSSMM7EL4X; 3AB9D23F7A4B3CSS=jdd03FF4E4BDXGZSCBQK57J2TX6ZV4SREWT4PR5ZTPR5BPS3MSTOCFHOTBP7M54NDGDSJ4DNM7GCDWLKYDSNAHWHKJOVO3IAAAAMOXNNEZWIAAAAAD7QV75X3YTIEHYX; _gia_d=1; __jda=143920055.1712540199099870852045.1712540199.1712540199.1712540199.1; __jdb=143920055.2.1712540199099870852045|1.1712540199; __jdc=143920055; shshshfpa=0540f07b-16b6-44cc-7dd3-364691a0a8ec-1712485527; shshshfpx=0540f07b-16b6-44cc-7dd3-364691a0a8ec-1712485527; qrsc=1; rkv=1.0; areaId=1; shshshfpb=BApXeisRSuOtAtl5r5PGqeM8aXzEqZhk2BlAJUghu9xJ1Mi5PD4C2; 3AB9D23F7A4B3C9B=FF4E4BDXGZSCBQK57J2TX6ZV4SREWT4PR5ZTPR5BPS3MSTOCFHOTBP7M54NDGDSJ4DNM7GCDWLKYDSNAHWHKJOVO3I";
        Connection connect = Jsoup.connect(url);
        connect.header("cookie",str);
        //解析网页
        Document document = connect.get();
        Element element = document.getElementById("J_goodsList");
//        System.out.println(element.html());
        //获取所有的li元素
        Elements elements = element.getElementsByTag("li");
        List<Content> goodList = new ArrayList<>();
        //获取元素中的内容，这里的el就是每个li标签
        for (Element el : elements) {
//            对于图片特别多的网站，一般都采用懒加载的方式，因此需要获取懒加载的图片地址：source-data-lazy-img，data-lazy-img
            String img = el.getElementsByTag("img").eq(0).attr("data-lazy-img");
            String price = el.getElementsByClass("p-price").eq(0).text();
            String title = el.getElementsByClass("p-name").eq(0).text();
            Content content = new Content();
            content.setImg(img);
            content.setTitle(title);
            content.setPrice(price);
            goodList.add(content);
        }
        return goodList;
    }
}
